<template>
    <div>
        <span class="headerTitle">{{ route.query.title }}</span>
        <el-divider border-style="dashed" />
        <div class="testTitle" v-if="singleChoice.length > 0"><span class="icon"></span><span class="label">单选题</span></div>
        <ul>
            <li v-for="(item, index) in singleChoice" :key="index">
                <p class="testName">{{ index + 1 }}. {{ item.questionName }}</p>
                <el-radio-group v-model="item.userAnswer" class="shuStyle">
                    <el-radio label="A" v-if="item.A"><span class="value"> A. {{ item.A }}</span></el-radio>
                    <el-radio label="B" v-if="item.B"><span class="value"> B. {{ item.B }}</span></el-radio>
                    <el-radio label="C" v-if="item.C"><span class="value"> C. {{ item.C }}</span></el-radio>
                    <el-radio label="D" v-if="item.D"><span class="value"> D. {{ item.D }}</span></el-radio>
                    <el-radio label="E" v-if="item.E"><span class="value"> E. {{ item.E }}</span></el-radio>
                    <el-radio label="F" v-if="item.F"><span class="value"> F. {{ item.F }}</span></el-radio>
                </el-radio-group>
            </li>
        </ul>
        <div class="testTitle" v-if="multipleSelection.length > 0"><span class="icon"></span><span class="label">多选题</span>
        </div>
        <ul>
            <li v-for="item, index in multipleSelection" :key="index">
                <p class="testName">{{ index + 1 }}. {{ item.questionName }}</p>
                <el-checkbox-group v-model="item.userAnswer" class="shuStyle">
                    <el-checkbox label="A" v-if="item.A"><span class="value"> A. {{ item.A }}</span></el-checkbox>
                    <el-checkbox label="B" v-if="item.B"><span class="value"> B. {{ item.B }}</span></el-checkbox>
                    <el-checkbox label="C" v-if="item.C"><span class="value"> C. {{ item.C }}</span></el-checkbox>
                    <el-checkbox label="D" v-if="item.D"><span class="value"> D. {{ item.D }}</span></el-checkbox>
                    <el-checkbox label="E" v-if="item.E"><span class="value"> E. {{ item.E }}</span></el-checkbox>
                    <el-checkbox label="F" v-if="item.F"><span class="value"> F. {{ item.F }}</span></el-checkbox>
                </el-checkbox-group>
            </li>
        </ul>
        <div class="testTitle" v-if="judgment.length > 0"><span class="icon"></span><span class="label">判断题</span></div>
        <ul>
            <li v-for="item, index in judgment" :key="index">
                <p class="testName">{{ index + 1 }}. {{ item.questionName }}</p>
                <el-radio-group v-model="item.userAnswer" class="shuStyle">
                    <el-radio label="A" v-if="item.A"><span class="value"> A. {{ item.A }}</span></el-radio>
                    <el-radio label="B" v-if="item.B"><span class="value"> B. {{ item.B }}</span></el-radio>
                </el-radio-group>
            </li>
        </ul>
        <div style="text-align: center;">
            <span class="submit" @click="submit">提交</span>
        </div>

        <!--操作数据 -->
        <el-dialog v-model="open" v-if="open" append-to-body :title="title" width="500px">
            <div class="bgImg">
                <span class="score">{{ score }}</span>
            </div>
            <p class="scoreTime">参与时间{{ time }}</p>
            <p class="scoreTime">历史最高{{ scoreMax }}</p>
            <div style="text-align: center;">
                <span class="button" @click="handleAnsows">查看答案</span>
                <span class="button yellowColor" @click="handleView">查看排行榜</span>
                <span class="button" @click="handleCertificate">我的证书</span>
                <span class="button" @click="handleScore">分数曲线图</span>
            </div>
        </el-dialog>

        <el-dialog custom-class="scoreDialog" v-model="dialogVisible" title="证书" width="800px" append-to-body>
            <img :src="dialogImageUrl" style="display: block; max-width: 100%; margin: 0 auto" />
        </el-dialog>
    </div>
</template>

<script setup name="practice">
import { ElMessage, ElMessageBox } from 'element-plus'
import { getTestById, submitText } from '@/api/knowledgeLearn/online/onlineClassroom'
import { onMounted } from 'vue';

const { proxy } = getCurrentInstance();
const route = useRoute();
const router = useRouter();
const singleChoice = ref([]); // 单选
const multipleSelection = ref([]); // 多选
const judgment = ref([]); // 判断
const errorMessage1 = ref([]);
const errorMessage2 = ref([]);
const errorMessage3 = ref([]);
const open = ref(false);
const title = ref('');
const score = ref(90);
const scoreMax = ref(90);
const time = ref('2023-4-35');
const testId = ref('');
const certificateId = ref('');

const dialogVisible = ref(false);
const dialogImageUrl = ref('');

watch(() => route, routes => {
    if (routes.name == 'practice') {
        getTopic();
    } else {
        sessionStorage.removeItem('testData')
    }
}, { deep: true, immediate: true });

// 获取题目
function getTopic() {
    const testDatas = JSON.parse(sessionStorage.getItem('testData')) ? JSON.parse(sessionStorage.getItem('testData')) : '';
    if (testDatas) {
        singleChoice.value = testDatas.singleChoice ? testDatas.singleChoice : [];
        multipleSelection.value = testDatas.multipleSelection ? testDatas.multipleSelection : [];
        judgment.value = testDatas.judgment ? testDatas.judgment : [];
    } else {
        getTestById(route.query).then(res => {
            singleChoice.value = res.data.singleChoice ? res.data.singleChoice : [];
            multipleSelection.value = res.data.multipleSelection ? res.data.multipleSelection : [];
            judgment.value = res.data.judgment ? res.data.judgment : [];
            sessionStorage.setItem('testData', JSON.stringify(res.data));
        })
    }
}

// 是否提示错误信息
function isError() {
    errorMessage1.value = [];
    errorMessage2.value = [];
    errorMessage3.value = [];
    if (singleChoice.value) {
        singleChoice.value.forEach((item, index) => {
            if (!item.userAnswer) {
                errorMessage1.value.push(index + 1)
            }
        })
    }
    if (multipleSelection.value) {
        multipleSelection.value.forEach((item, index) => {
            if (!item.userAnswer || item.userAnswer.length == 0) {
                errorMessage2.value.push(index + 1)
            }
        })
    }
    if (judgment.value) {
        judgment.value.forEach((item, index) => {
            if (!item.userAnswer) {
                errorMessage3.value.push(index + 1)
            }
        })
    }
    if ((errorMessage1.value.length != 0) || (errorMessage2.value != 0) || (errorMessage3.value != 0)) {
        return true;
    } else {
        return false;
    }
}

// 提交
function submit() {
    const errorFlag = isError();
    if (errorFlag) {
        const message1 = errorMessage1.value.length != 0 ? `<p>单选题 第${errorMessage1.value.join('、')}题未选择</p>` : '';
        const message2 = errorMessage2.value != 0 ? `<p>多选题 第${errorMessage2.value.join('、')}题未选择</p>` : '';
        const message3 = errorMessage3.value != 0 ? `<p>判断题 第${errorMessage3.value.join('、')}题未选择</p>` : '';
        ElMessageBox.alert(`${message1}${message2}${message3}`, '消息', {
            type: 'error',
            confirmButtonText: '确认',
            dangerouslyUseHTMLString: true,
        })
    } else {
        if (multipleSelection.value) {
            const multipleSelectionList = [];
            multipleSelection.value.map((item, index) => {
                multipleSelectionList.push({ ...item, userAnswer: item.userAnswer.join(',') })
            })
            submitText({ list: [...singleChoice.value, ...multipleSelectionList, ...judgment.value], courseId: route.query.id }).then(res => {
                title.value = `得分${res.data.score}很棒呀！`;
                score.value = res.data.score;
                scoreMax.value = res.data.maximumScore;
                time.value = res.data.time;
                testId.value = res.data.testId;
                certificateId.value = res.data.certificateId;
                open.value = true;
            })
        } else {
            submitText({ list: [...singleChoice.value, ...judgment.value], courseId: route.query.id }).then(res => {
                title.value = `得分${res.data.score}很棒呀！`;
                score.value = res.data.score;
                scoreMax.value = res.data.maximumScore;
                time.value = res.data.time;
                testId.value = res.data.testId
                open.value = true;
            })
        }
    }
}

// 查看排行榜
function handleView() {
    router.push({
        path: '/knowledgeLearn/online/practiceInClass/list',
    })
}
// 查看证书
function handleCertificate() {
    if (certificateId.value) {
        dialogImageUrl.value = import.meta.env.VITE_APP_BASE_API + "/attachment/downLoadFile?attachId=" + certificateId.value;
        dialogVisible.value = true;
    } else {
        proxy.$modal.msgWarning("很遗憾，您未获得证书");
    }
}
// 查看答案
function handleAnsows() {
    router.push({
        path: '/knowledgeLearn/online/practiceInClass/checkAnswer',
        query: {
            id: testId.value,
            title: route.query.title,
        }
    })
}

// 查看分数曲线图
function handleScore() {
    router.push({
        path: '/knowledgeLearn/online/practiceInClass/fractionalGraph',
    })
}

// onMounted(() => {
//     getTopic();
// })
</script>

<style scoped lang="scss">
.headerTitle {
    display: inline-block;
    height: 100px;
    line-height: 100px;
    font-weight: bold;
    font-size: 30px;
    color: #333;
}

:deep(.el-divider) {
    margin: 0px;
}

.testTitle {
    margin: 30px 0px;
    height: 24px;
    display: flex;
    align-items: center;
    flex-direction: row;
    color: #107ce5;

    .icon {
        display: inline-block;
        width: 5px;
        height: 25px;
        background-color: #107ce5;
    }

    .label {
        font-size: 24px;
        font-weight: bold;
        margin-left: 10px;
    }
}

ul {
    list-style-type: none;
    margin: 0px !important;
    padding-left: 0px !important;

    li {
        margin-bottom: 28px;

        p {
            margin: 0px !important;
        }

        .shuStyle {
            display: flex;
            flex-direction: column;
            align-items: flex-start;
            margin-left: 30px;
        }

        .value {
            font-size: 20px;
            font-weight: 400;
            letter-spacing: 1.2;
        }

        .testName {
            font-weight: bold;
            font-size: 20px;
            margin-bottom: 12px !important;
        }
    }
}

.submit {
    display: inline-block;
    width: 150px;
    height: 50px;
    line-height: 50px;
    background: #107ce5;
    border: 1px solid #707070;
    border-radius: 4px;
    opacity: 1;
    color: #fff;
    font-size: 20px;
    font-weight: 400;
    cursor: pointer;
}

:deep(.el-dialog) {
    :deep(.el-dialog__body) {
        padding: 0 20px !important;
    }
}

.bgImg {
    width: 275px;
    height: 241px;
    border-radius: 0px;
    opacity: 1;
    background-image: url('../../../../assets/icon/score.png');
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
    margin: 0 auto;
    text-align: center;
}

.scoreTime {
    text-align: center;
    font-size: 24px;
    color: #b4893a;
}

.score {
    color: #e34b00;
    font-weight: bold;
    font-size: 56px;
    position: relative;
    top: 64px;
}

.button {
    display: inline-block;
    text-align: center;
    line-height: 50px;
    width: 180px;
    height: 50px;
    border: 1px solid #999;
    border-radius: 25px;
    opacity: 1;
    color: #666666;
    font-size: 24px;
    margin: 10px;
    cursor: pointer;
}

.yellowColor {
    background-image: linear-gradient(to right, #be9c5e, #ad8437);
    color: #fff;
}

.scoreDialog {
    background: #b4893a !important;
}

:deep(.el-dialog .el-dialog__header) {
    background-color: red;
}
</style>